﻿using MvcApplication;
using MvcApplication.service;
using MvcEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication4MVC.request;

namespace WebApplication4MVC.Controllers
{
    public class UserController : BaseController
    {

        DemoService demoService = new DemoService();

        // GET: User
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult UserList(querylist requst) {
            var result = new ResponseReslutPage();
            List<DemoUser> demoUsers = demoService.GetList(requst.UserName, requst.UserPhone, out int total, requst.PageSize, requst.PageNum);
            List<UserInfoModel> userinfos = new List<UserInfoModel>();
            if (demoUsers!=null&& demoUsers.Count>0)
            {
                userinfos = demoUsers.Select(x => new UserInfoModel
                {
                    Id=x.Id,
                    UserName=x.UserName,
                    UserPhone=x.UserPhone,
                    LastDate=x.LastDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    CreateDate = x.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    LoginAt = x.LoginAt,
                }).ToList();
            }

            result.code = 200;
            result.msg = "ok";
            result.data = userinfos;
            result.total = total;
            return Json(result);
        }

        public ActionResult UserListIndex(string UserName,string UserPhone) {
            List<DemoUser> demoUsers = demoService.GetList(UserName, UserPhone, out int total, 10, 1);
            ViewBag.demoUsers = demoUsers;
            return View();
        }

        public ActionResult deleteRow(string id)
        {
            var reslt = new ResponseReslut();
            UserInfoModel loginser = Session["LoginUserInfo"] as UserInfoModel;
            if (loginser == null)
            {
                reslt.code = 401;
                reslt.msg = "授权失败，请登录！";
                return Json(reslt);
            }
            var result = demoService.deleteUser(id, loginser.Id);
            return Json(result);
        }

        public ActionResult ModifyPwd(ModifyPwdReqeust reqeust) {

            if (reqeust==null)
            {
               return Json(CreateResponseReslut.CreateFail("参数错误"));
            }
            if (string.IsNullOrWhiteSpace(reqeust.Id))
            {
                return Json(CreateResponseReslut.CreateFail("参数错误,标识不正确！"));
            }

            if (string.IsNullOrWhiteSpace(reqeust.OldPwd))
            {
                return Json(CreateResponseReslut.CreateFail("原密码不正确！"));
            }

            if (string.IsNullOrWhiteSpace(reqeust.NewPwd)|| string.IsNullOrWhiteSpace(reqeust.ConfirmNewPwd))
            {
                return Json(CreateResponseReslut.CreateFail("新的密码必须填写！"));
            }

            if (reqeust.NewPwd!=reqeust.ConfirmNewPwd)
            {
                return Json(CreateResponseReslut.CreateFail("新密码不一致！"));
            }

            //调用数据库修改密码.
            var reslut = demoService.ModifyPwd(reqeust.Id,reqeust.OldPwd,reqeust.NewPwd);
            return Json(reslut);
        }

        public ActionResult ModifyPhone(ModifyPhoneRequest request) 
        {
            var resul = new ResponseReslut();
            if (request == null)
            {
                return Json(CreateResponseReslut.CreateFail("参数错误"));
            }
            if (string.IsNullOrWhiteSpace(request.Id))
            {
                return Json(CreateResponseReslut.CreateFail("参数错误，不能修改"));
            }

            if (string.IsNullOrWhiteSpace(request.UserPhone))
            {
                return Json(CreateResponseReslut.CreateFail("手机号不能空"));
            }
            if (request.UserPhone.Length<11)
            {
                return Json(CreateResponseReslut.CreateFail("手机号格式不正确"));
            }

            var reslut = demoService.ModifyPhone(request.Id, request.UserPhone);
            return Json(reslut);

        }

    }
}